import {LinkDto} from '../entity/link-dto';
import {Menu} from '../entity/menu';

export class SettingUtils {
    public static getSettingCrumbs(currentCrumb: LinkDto): LinkDto[] {
        const crumbs: LinkDto[] = <LinkDto[]>[{
            text: '项目索引',
            routerLink: [''],
            frontIcon: 'home'
        }];
        crumbs.push(<LinkDto>currentCrumb);
        return crumbs;
    }

    public static getSettingLeftNavs(currentMenu: string): Menu[] {
        const menus: Menu[] = <Menu[]>[{
            index: 0,
            id: 'user',
            name: '用户',
            children: [{
                id: 'user-info',
                name: '用户信息',
                url: ['/my/info']
            }]
        }];

        SettingUtils.checkActive(menus, currentMenu);
        return menus;
    }

    private static checkActive(menus: Menu[], currentMenu: string): boolean {
        let active = false;
        menus.forEach(menu => {
            if (!active && menu.children) {
                active = SettingUtils.checkActive(menu.children, currentMenu);
            }
            if (active || menu.id === currentMenu) {
                menu.expanding = true;
                menu.active = true;
                active = true;
            }
        });
        return active;
    }
}
